package com.zy.web.servlet;

import com.zy.pojo.Department;
import com.zy.pojo.ResultInfo;
import com.zy.pojo.Salary;
import com.zy.service.SalaryService;
import com.zy.service.impl.SalaryServiceImpl;
import com.zy.web.servlet.base.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2021/6/5
 * Time: 下午 13:47
 * Description: 工资控制器
 */
@WebServlet("/salary/*")
public class SalaryServlet extends BaseServlet {

    private SalaryService salaryService = new SalaryServiceImpl();

    public void getAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer page = null;
        Integer limit = null;
        Integer startMoney = null;
        Integer endMoney = null;
        try {
            page = Integer.parseInt(request.getParameter("page"));
            limit = Integer.parseInt(request.getParameter("limit"));
            startMoney = Integer.parseInt(request.getParameter("startMoney"));
            endMoney = Integer.parseInt(request.getParameter("endMoney"));
        } catch (NumberFormatException e) {

        }
        String empName = request.getParameter("empName");
        String roleName = request.getParameter("roleName");
        String departmentName = request.getParameter("departmentName");
        Salary salary = new Salary();
        salary.setStart(page);
        salary.setLimit(limit);
        salary.setRoleName(roleName);
        salary.setDepartmentName(departmentName);
        salary.setStartMoney(startMoney);
        salary.setEndMoney(endMoney);
        salary.setEmpName(empName);
        List<Salary> list = salaryService.getSalaryListByPage(salary);
        Integer count = salaryService.getSalaryCount(salary);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("message", "操作成功");
        map.put("count", count);
        map.put("data", list);
        writeValue(map, response);
    }

    public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer empId = null;
        Double money = null;
        try {
            empId = Integer.parseInt(request.getParameter("empId"));
            money = Double.parseDouble(request.getParameter("money"));
        } catch (NumberFormatException e) {
            System.out.println("添加工资");
        }
        Salary salary = new Salary();
        salary.setEmpId(empId);
        salary.setMoney(money);
        System.out.println(salary);
        Salary flag = salaryService.getSalaryByEmpId(empId);
        boolean result = false;
        if (flag == null) {
            //添加
            result = salaryService.insertSalary(salary);
        } else {
            //更新
            result = salaryService.updateSalary(salary);
        }
        ResultInfo resultInfo = new ResultInfo();
        if (result) {
            resultInfo.setCode(200);
            resultInfo.setMessage("操作成功");
        } else {
            resultInfo.setCode(400);
            resultInfo.setMessage("操作失败");
        }
        writeValue(resultInfo, response);
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    public void average(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

}